import 'package:flutter/material.dart';
import 'package:flutter_flexible/utils/gps/gps_util.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'dart:async';
import 'dart:io';
import 'dart:ui';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter_background_service/flutter_background_service.dart';
import 'package:geolocator/geolocator.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../utils/gps/gps_util.dart';
import '../../utils/tool/log_util.dart';
import '../../services/gps_service.dart';


// to ensure this is executed
// run app from xcode, then from xcode menu, select Simulate Background Fetch

@pragma('vm:entry-point')
Future<bool> onIosBackground(ServiceInstance service) async {
  WidgetsFlutterBinding.ensureInitialized();
  DartPluginRegistrant.ensureInitialized();

  SharedPreferences preferences = await SharedPreferences.getInstance();
  await preferences.reload();
  final log = preferences.getStringList('log') ?? <String>[];
  log.add(DateTime.now().toIso8601String());
  await preferences.setStringList('log', log);

  print("苹果后台执行");


  return true;
}

@pragma('vm:entry-point')
void onStart(ServiceInstance service) async {
  // Only available for flutter 3.0.0 and later
  DartPluginRegistrant.ensureInitialized();

  // For flutter prior to version 3.0.0
  // We have to register the plugin manually

  SharedPreferences preferences = await SharedPreferences.getInstance();
  await preferences.setString("hello", "world");

  /// OPTIONAL when use custom notification
  final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
  FlutterLocalNotificationsPlugin();

  if (service is AndroidServiceInstance) {
    service.on('setAsForeground').listen((event) {
      service.setAsForegroundService();
    });

    service.on('setAsBackground').listen((event) {
      service.setAsBackgroundService();
    });
  }

  service.on('stopService').listen((event) {
    service.stopSelf();
  });

  // Position lp = Position(longitude: 0
  //     , latitude: 0
  //     , timestamp: new DateTime(2025)
  //     , accuracy: 0
  //     , altitude: 0
  //     , altitudeAccuracy: 0
  //     , heading: 0
  //     , headingAccuracy: 0
  //     , speed: 0
  //     , speedAccuracy: 0);

  // bring to foreground
 Timer.periodic(const Duration(seconds: 5), (timer) async {



   print("work manager task start");
   // Future<Position> fp = GpsUtil.determinePosition();
   // fp.then((p) {
   //   LogUtil.d('work manager task 获取到的GPS：${p.longitude} : ${p.latitude}');
   //   postLocation({"longitude":p.longitude,"latitude":p.latitude});
   //
   // }).catchError((msg){
   //   LogUtil.d("work manager 读取失败");
   // });


    if (service is AndroidServiceInstance) {
      if (await service.isForegroundService()) {
        /// OPTIONAL for use custom notification
        /// the notification id must be equals with AndroidConfiguration when you call configure() method.
        // flutterLocalNotificationsPlugin.show(
        //   888,
        //   'COOL SERVICE',
        //   'Awesome 位置',
        //   const NotificationDetails(
        //     android: AndroidNotificationDetails(
        //       '位置获取',
        //       '前台',
        //       icon: 'ic_bg_service_small',
        //       ongoing: true,
        //     ),
        //   ),
        // );

        // if you don't using custom notification, uncomment this
        // service.setForegroundNotificationInfo(
        //   title: "位置获取",
        //   content: "Updated at ${DateTime.now()}",
        // );
      }
    }

    /// you can see this log in logcat
    debugPrint('FLUTTER BACKGROUND SERVICE: ${DateTime.now()}');

    GpsUtil.determinePosition().then((p){
      debugPrint("坐标${p.longitude} ,${p.latitude}");
    //  postLocation({"longitude":p.latitude,"latitude":p.latitude});
    });


    // test using external plugin
    final deviceInfo = DeviceInfoPlugin();
    String? device;
    if (Platform.isAndroid) {
      final androidInfo = await deviceInfo.androidInfo;
      device = androidInfo.model;
    } else if (Platform.isIOS) {
      final iosInfo = await deviceInfo.iosInfo;
      device = iosInfo.model;
    }

    service.invoke(
      'update',
      {
        "current_date": DateTime.now().toIso8601String(),
        "device": device,
      },
    );
 });
}

class BackgroundServiceDemo extends StatefulWidget {
  const BackgroundServiceDemo({super.key, this.params});
  final dynamic params;
  @override
  State<BackgroundServiceDemo> createState() => _BackgroundServiceDemoState();
}

class _BackgroundServiceDemoState extends State<BackgroundServiceDemo> {
  String text = "Stop Service";
  @override
  Widget build(BuildContext context) {
    init();
    return  Scaffold(
      appBar: AppBar(
        title: const Text('Service App'),
      ),
      body: Column(
        children: [
          StreamBuilder<Map<String, dynamic>?>(
            stream: FlutterBackgroundService().on('update'),
            builder: (context, snapshot) {
              if (!snapshot.hasData) {
                return const Center(
                  child: CircularProgressIndicator(),
                );
              }

              final data = snapshot.data!;
              String? device = data["device"];
              DateTime? date = DateTime.tryParse(data["current_date"]);
              return Column(
                children: [
                  Text(device ?? 'Unknown'),
                  Text(date.toString()),
                ],
              );
            },
          ),
          ElevatedButton(
            child: const Text("Foreground Mode"),
            onPressed: () =>
                FlutterBackgroundService().invoke("setAsForeground"),
          ),
          ElevatedButton(
            child: const Text("Background Mode"),
            onPressed: () =>
                FlutterBackgroundService().invoke("setAsBackground"),
          ),
          ElevatedButton(
            child: Text(text),
            onPressed: () async {
              final service = FlutterBackgroundService();
              var isRunning = await service.isRunning();
              isRunning
                  ? service.invoke("stopService")
                  : service.startService();

              setState(() {
                text = isRunning ? 'Start Service' : 'Stop Service';
              });
            },
          ),
          const Expanded(
            child: LogView(),
          ),
        ],
      ),
    );

  }


  Future<void> init() async {
    WidgetsFlutterBinding.ensureInitialized();
    await initializeService();
  }

  Future<void> initializeService() async {
    final service = FlutterBackgroundService();

    /// OPTIONAL, using custom notification channel id
    const AndroidNotificationChannel channel = AndroidNotificationChannel(
      'my_foreground', // id
      'MY FOREGROUND SERVICE', // title
      description:
      'This channel is used for important notifications.', // description
      importance: Importance.low, // importance must be at low or higher level
    );

    final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
    FlutterLocalNotificationsPlugin();

    if (Platform.isIOS || Platform.isAndroid) {
      await flutterLocalNotificationsPlugin.initialize(
        const InitializationSettings(
          iOS: DarwinInitializationSettings(),
          android: AndroidInitializationSettings('ic_bg_service_small'),
        ),
      );
    }

    await flutterLocalNotificationsPlugin
        .resolvePlatformSpecificImplementation<
        AndroidFlutterLocalNotificationsPlugin>()
        ?.createNotificationChannel(channel);

    await service.configure(
      androidConfiguration: AndroidConfiguration(
        // this will be executed when app is in foreground or background in separated isolate
        onStart: onStart,
        // auto start service
        autoStart: true,
        autoStartOnBoot:true,
        isForegroundMode: true,
        notificationChannelId: 'my_foreground',
        initialNotificationTitle: '位置服务',
        initialNotificationContent: '请勿关闭',
        foregroundServiceNotificationId: 888,
        foregroundServiceTypes: [AndroidForegroundType.location],
      ),
      iosConfiguration: IosConfiguration(
        // auto start service
        autoStart: true,
        // this will be executed when app is in foreground in separated isolate
        onForeground: onStart,
        // you have to enable background fetch capability on xcode project
        onBackground: onIosBackground,
      ),
    );
  }
}

class LogView extends StatefulWidget {
  const LogView({super.key});

  @override
  State<LogView> createState() => _LogViewState();
}

class _LogViewState extends State<LogView> {
  late final Timer timer;
  List<String> logs = [];

  @override
  void initState() {
    super.initState();
    timer = Timer.periodic(const Duration(seconds: 1), (timer) async {
      final SharedPreferences sp = await SharedPreferences.getInstance();
      await sp.reload();
      logs = sp.getStringList('log') ?? [];
      if (mounted) {
        setState(() {});
      }
    });
  }

  @override
  void dispose() {
    timer.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: logs.length,
      itemBuilder: (context, index) {
        final log = logs.elementAt(index);
        return Text(log);
      },
    );
  }
}